package com.project.service.impl;

import com.project.mapper.ModuleMapper;
import com.project.mapper.SubSystemMapper;
import com.project.model.entity.Module;
import com.project.model.entity.SubSystem;
import com.project.model.vo.ModulesValue;
import com.project.model.vo.ReportModules;
import com.project.service.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ModuleServiceImpl implements ModuleService {

    private final ModuleMapper moduleMapper;
    private final SubSystemMapper subsystemMapper;

    @Autowired
    public ModuleServiceImpl(ModuleMapper moduleMapper, SubSystemMapper subsystemMapper) {
        this.moduleMapper = moduleMapper;
        this.subsystemMapper = subsystemMapper;
    }

    @Override
    public Module addModule(Module module) {
        moduleMapper.insertModule(module);
        return module;
    }

    @Override
    public void deleteModule(Integer id) {
        moduleMapper.deleteModuleById(id);
    }

    @Override
    public Module updateModule(Module module) {
        moduleMapper.updateModule(module);
        return module;
    }

    @Override
    public Module getModuleById(Integer id) {
        return moduleMapper.selectModuleById(id);
    }

    @Override
    public List<Module> getAllModules() {
        return moduleMapper.selectAllModules();
    }

    @Override
    public List<Module> getModulesBySubsystemId(Integer subsystemId) {
        return moduleMapper.selectModulesBySubsytemId(subsystemId);
    }

    @Override
    public List<ReportModules> getModulesByReportId(Integer reportId) {
        List<ReportModules> res = new ArrayList<>();
        List<SubSystem> subSystems = subsystemMapper.selectSubSystemsByReportId(reportId);
        for (SubSystem sub : subSystems) {
            List<Module> modules = moduleMapper.selectModulesBySubsytemId(sub.getId());

            List<ModulesValue> mVal = new ArrayList<>();
            for (Module module : modules) {
                ModulesValue modulesValue = new ModulesValue(module.getId(), module.getName(), module.getFunctionPointNum());
                mVal.add(modulesValue);
            }
            ReportModules rm = new ReportModules(sub.getName(), mVal);
            res.add(rm);
        }
        for (int i = 0; i < res.size(); i++) {
            System.out.println(res.get(i));
        }
        return res;
    }
}
